/***************************************************************************
创建者：华磊
 开始时间：               : 2020.1１.２４
 copyright            : (C) 深圳市华友高科有限公司
 修改说明：（每次有修改就添加一条，带有 修改人，修改时间，修改描述）

 ***************************************************************************
 *   io窗口程序
*                                                                         *
 ***************************************************************************/
#include "pointregisterstatus.h"
#include "ui_pointregisterstatus.h"
#include "systemstatus.h"
#include <QStandardItemModel>
#include <QStringListModel>
#include "hmicommondefine.h"
#include "instructionEnum.h"


PointRegisterStatus::PointRegisterStatus(int styleId,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PointRegisterStatus)
{
    ui->setupUi(this);
    initialDisplayStyle(styleId);

    setTableView();

    initFlag = true;
}

PointRegisterStatus::~PointRegisterStatus()
{
    delete ui;
}
void PointRegisterStatus::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        ui->listView->setGeometry(10,35,85,350);
        ui->tableView->setGeometry(100,35,350,350);
        ui->pushButton_3->setGeometry(10,390,70,50);
        ui->pushButton_4->setGeometry(95,390,70,50);
        ui->pushButton_2->setGeometry(180,390,66,50);
        ui->pushButton_5->setGeometry(260,390,80,50);
        ui->pushButton_6->setGeometry(360,390,96,50);
        break;
    default:
        break;
    }
}

void PointRegisterStatus::changeLanguage()
{
    ui->retranslateUi(this);
    setTableView();
}

void PointRegisterStatus::setTableView()
{
    model = new QStandardItemModel();
    ui->tableView->setModel(model);
    model->setColumnCount(2);
    ui->tableView->setColumnWidth(0,155);
    ui->tableView->setColumnWidth(1,165);
    ui->tableView->verticalHeader()->hide();
    model->setHeaderData(0,Qt::Horizontal,tr("类型"));
    model->setHeaderData(1,Qt::Horizontal,tr("值"));
    model->setItem(0,0,new QStandardItem(tr("节点类型")));
    model->setItem(1,0,new QStandardItem(tr("点编号")));
    model->setItem(2,0,new QStandardItem(tr("组编号")));
    model->setItem(3,0,new QStandardItem(tr("用户坐标系编号")));
    model->setItem(4,0,new QStandardItem(tr("工具坐标系编号")));
    model->setItem(5,0,new QStandardItem(tr("多解选择;多圈选择")));
    model->setItem(6,0,new QStandardItem(""));
    model->setItem(7,0,new QStandardItem(""));
    model->setItem(8,0,new QStandardItem(""));
    model->setItem(9,0,new QStandardItem(""));
    model->setItem(10,0,new QStandardItem(""));
    model->setItem(11,0,new QStandardItem(""));
    model->setItem(12,0,new QStandardItem(""));
    model->setItem(13,0,new QStandardItem(""));
    model->setItem(14,0,new QStandardItem(""));
    model->setItem(15,0,new QStandardItem(""));//预先开启16个位置
    model->setItem(16,0,new QStandardItem(""));//预先开启16个位置
    model->setItem(17,0,new QStandardItem(""));//预先开启16个位置

    for(int i = 0;i<18;i++)
    {
        model->setItem(i,1,new QStandardItem(""));
    }

    model2 = NULL;

    if(6 != sixAxisName.size())
    {
        sixAxisName.resize(6);
        sixAxisName[0] = tr("轴值:X");
        sixAxisName[1] = tr("轴值:Y");
        sixAxisName[2] = tr("轴值:Z");
        sixAxisName[3] = tr("轴值:W");
        sixAxisName[4] = tr("轴值:P");
        sixAxisName[5] = tr("轴值:R");
    }

    initFlag = true;
}

void PointRegisterStatus::setCurrentRowToShow(int t)
{
    if(t < model2->rowCount() && t >= 0)
    {
        QModelIndex index = model2->index(t);
        ui->listView->setCurrentIndex(index);

        on_listView_clicked(index);
    }
    else
    {
        //messageBoxShow(tr("提示"),tr("您所查看的点信息不存在"));
    }
}

void PointRegisterStatus::on_pushButton_clicked()
{
    int t =ui->lineEdit->text().toInt();
    setCurrentRowToShow(t-1);
}


void PointRegisterStatus::on_pushButton_3_pressed()
{
    if(ui->listView->currentIndex().row()>=0)
    {
        int i=ui->listView->currentIndex().row();//下标
        emit jMove(i);
    }
    else
    {

    }
}

void PointRegisterStatus::on_pushButton_4_pressed()
{
    if(ui->listView->currentIndex().row()>=0)
    {
        int i=ui->listView->currentIndex().row();//下标
        emit lMove(i);
    }
    else
    {

    }
}

void PointRegisterStatus::on_pushButton_3_released()
{
    emit moveStop();
}

void PointRegisterStatus::on_pushButton_4_released()
{
    emit moveStop();
}

void PointRegisterStatus::on_pushButton_2_clicked()
{
    int i=ui->listView->currentIndex().row();
    if(i >= 0)
    {
        emit recordPoint(i);
    }
    else
    {

        //没有选中
    }
}

void PointRegisterStatus::on_pushButton_5_clicked()
{
    if(ui->listView->currentIndex().row()>=0)
    {
        int i=ui->listView->currentIndex().row();
        emit changePointTypeToJoint(i);
    }
    else
    {

    }
}

void PointRegisterStatus::on_pushButton_6_clicked()
{
    if(ui->listView->currentIndex().row()>=0)
    {
        int i=ui->listView->currentIndex().row();
        emit changePointTypeToCartesian(i);
    }
    else
    {

    }
}

void PointRegisterStatus::on_pushButton_F5_clicked()
{
    //prChangeFlag = true;
    emit pointRegisterStatusFlagSignal();
}

void PointRegisterStatus::showPointRegisterStatusView()
{

}

void PointRegisterStatus::updateView()
{
    int n = pointList.size();

    if(n>0)
    {
        if(initFlag)
        {
            QStringList list;
            for(int i=0;i<n;i++)
            {
                list+=QString("%1").arg(i+1);

            }

            model2 = new QStringListModel(list);
            ui->listView->setModel(model2);
        }
        else
        {
            //for(int i=0;i<n;i++)
            //{
                //if(model->item(i,1)->text() != QString::number(row[i]))
                //{
                //    model->item(i,1)->setText(QString::number(row[i]));
                //}
            //}
            //if(f5Flag)
        }
    }

    initFlag = false;//初始化完成

}

void PointRegisterStatus::on_listView_clicked(const QModelIndex &index)
{
    //emit pointRegisterStatusSearch();//点击了才触发?实际已无效,可注释
    int n=index.row();//当前点中行

    QString n1;
    int type = pointList[n].jointFlag;
    switch(type)
    {
        case EM_CF_DEG_TYPE:
            n1=tr("关节");
            break;
        case EM_CF_WORLD_TYPE:
            n1=tr("笛卡尔");
            break;
    default:
            break;
    }

    //model->reset();
    model->item(0,1)->setText(n1);
    model->item(1,1)->setText( QString::number(pointList[n].pointNum));
    model->item(2,1)->setText( QString::number(pointList[n].groupNum));
    model->item(3,1)->setText( QString::number(pointList[n].ufValue ));
    model->item(4,1)->setText( QString::number(pointList[n].utValue ));
    model->item(5,1)->setText( tr(pointList[n].configString.data()));

    int rowNum = 5;

    int size=pointList[n].positionValue.size();
    for(int i=0;i<size;i++)
    {
        QString line=QString::number(pointList[n].positionValue[i]);
        if(EM_CF_DEG_TYPE == type)
        {
            model->item(6+i,0)->setText(tr("轴值:J")+QString::number(i+1));
            model->item(6+i,1)->setText(line);
        }
        else
        {
            if(i<6)
            {
                model->item(6+i,0)->setText(sixAxisName[i]);
                model->item(6+i,1)->setText(line);
            }
        }

        rowNum++;
    }

//    QString total2 = "";
//    size=pointList[n].extPositionValue.size();
//    for(int i=0;i<size ;i++)
//    {
//        QString line=QString::number(pointList[n].extPositionValue[i]);
//        total2=line+"; "+total2;
//    }
//    rowNum++;//+1
//    model->item(rowNum,0)->setText(tr("外部轴值"));
//    model->item(rowNum,1)->setText(total2);
    for(int i=0;i<pointList[n].extPositionValue.size();i++)
    {
        rowNum++;//+1
        model->item(rowNum,0)->setText(tr("外部轴值:E")+QString::number(i+1));
        model->item(rowNum,1)->setText(QString::number(pointList[n].extPositionValue[i]));
    }

    rowNum++;
    if(rowNum<18)
    {
        for(int i = rowNum;i<18;i++)
        {
            model->item(i,0)->setText("");
            model->item(i,1)->setText("");
        }
    }

}
